import {getCityInfoAPI} from "../apis/list";

export const getCurrentCity = () => {
    // 判断localStorage是否有定位城市
    const localCity = JSON.parse(localStorage.getItem('hkzf_city'));
    if (!localCity) {
        return new Promise((resolve, reject) => {
            // 如果没有，使用首页获取定位城市来获取
            const curCity = new window.BMapGL.LocalCity()
            curCity.get(async res => {
                try {
                    const result = await getCityInfoAPI(res.name);
                    // 存储在localStorage
                    localStorage.setItem('hkzf_city', JSON.stringify(result.data.body))
                    resolve(result.data.body)
                } catch (e) {
                    // 获取定位城市失败
                    reject(e)
                }
            })
        })
    }
    // 如果有，直接返回localStorage中的城市数据
    return Promise.resolve(localCity)
}